Thread: Function to randomize what's inside #RANDOM(1|2|3|4)#

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    Function to randomize what's inside #RANDOM(1|2|3|4)#

    Hi, I use those forums for about 2 weeks when I first start codding in C but I think this is the time to register .

    I have a problem with my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char replacerand(char *str);
    
    
    int main() {
      char str[] = "f hgfgh #RANDOM(David Lai|9123 4567|Dav|valu e1|val ue2|valu e3|value4|value5)#\n"
      "fdslk gfdlkgfd #RANDOM(mom1|fgds|ggg|gfdgfd)# jkfgjk hgkj\n";
    
    
    
      replacerand(str);
    }
    
    
    char replacerand(char *str) {
         int i;
         int count = 0;
         char string33[200];
         char string34[200];
         sscanf(str, "#RANDOM(%s%[^)]", string33, string34);
    
    
         sprintf(str, "%s%s", string33, string34);
         printf("\n\n\n--- %s ---\n", str);
         const char delims[] = "|";
         char *result = NULL;
         char **store = NULL;
         char **tmp = NULL;
         char *name = NULL;
         char *telephone = NULL;
         char *nickname = NULL;
         int randomnum;
         int totalval;
    
    
         result = strtok(str, delims);
    
    
         while (result != NULL) {
              free(tmp);
              tmp = malloc(count * sizeof(char *));
              for (i=0; i<count; i++) {
                   tmp[i] = store[i];
              }
              free(store);
              store = malloc((count + 1) * sizeof(char *));
              for (i=0; i<count; i++) {
                   store[i] = tmp[i];
              }
              store[count] = result;
              //printf("%s", store[1]);
              count++;
              //printf(".%s\n", result);
              result = strtok(NULL, delims);
         }
         int f;
         srand(time(NULL));
         //srand(time(NULL));
         randomnum = rand() % count;
         printf("%s %d %d\n", store[randomnum], count, randomnum);
         free(tmp);
         free(store);
         return 0;
    }
    First I'm trying to transform every #RANDOM(whatever) into arr to a random value delimited by a "|".

    I'm just not ableto transform them all since the sscanf() do not seems to understand what I'm trying to do.

    Anyone ?
    Last edited by NsoftDev; 05-31-2012 at 05:06 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Firstly, any code that attempts to modify a string literal yields undefined behaviour (for which a common practical symptom is a crashing program). Your main() function is passing a string literal to replacerand() which, in turn, attempts to change it.

    That issue aside (which is a fairly significant one, but not hard to avoid) why not just do the steps of (1) search for the string "#RANDOM(" (2) search for the closing ')' (3) extract the text in between (4) parse that text in between (5) replace the text "#RANDOM(<whatever>" with whatever is required? No need to use any of the functions like sprintf(), sscanf(), at all .... simple loops will work. Given that you will be replacing "#RANDOM(<whatever>" with some substring of <whatever> you should not need to use dynamic memory allocation either for scratch space. At worst, some of those loops might replace simple functions from the standard library .... but if you can't write code by hand to do what you want, you won't be able to work out how to use standard library functions to do it either.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    Hi thanks for your answer, In fact i'm trying to port my perl program into C and I realise that it's harder to do something that was very easy in perl.

    on my perl program it took few lines of code to completely do what I wanted:

    Code:
    #!/usr/bin/perl
    
    
    $text = "f hgfgh #RANDOM(David Lai::9123 4567::Dav::valu el::val ue2::valu e3)# gfgfd hg".
            "fdslk gfdlkgfd #RANDOM(mom1::fgds::ggg::gfdgfd)# jkfgjk hgkj\n";
    
    
    while($text =~ /#RANDOM\((.*?)\)#/g)
    {
      $splitsingle = $1;
      @splitallsingle = split(/\::/, $splitsingle);
      $maxrandom_number = 0;
      $maxrandom_number = $maxrandom_number + scalar(@splitallsingle);
      $random_number = int(rand($maxrandom_number));
      $text =~ s/#RANDOM\($splitsingle\)#/$splitallsingle[$random_number]/g;
    }
    
    
    print "$text\n";
    Is there a way to do it this way in C ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    PCRE - Perl Compatible Regular Expressions
    Link with the pcre, and you can use the same Perl expressions to match things in your C code, as you do in your Perl code.

    There's a bit of book keeping to set things up, and making sure there is space for all the matched sub-expressions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2011, 09:10 PM
  2. random/randomize
    By Lynn in forum C Programming
    Replies: 6
    Last Post: 06-05-2006, 03:25 PM
  3. Replies: 4
    Last Post: 01-29-2005, 11:57 AM
  4. need suggestion for custom randomize function
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2002, 04:59 AM
  5. Randomize Number Function
    By beyonddc in forum C Programming
    Replies: 3
    Last Post: 12-10-2001, 04:31 AM

Tags for this Thread